package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 根据时间段统计营业数据
     *
     * 营业额：当日已完成订单的总金额
     * 有效订单：当日已完成订单的数量
     * 订单完成率：有效订单数 / 总订单数
     * 平均客单价：营业额 / 有效订单数
     * 新增用户：当日新增用户的数量
     * @param begin
     * @param end
     * @return
     */

    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //统计总的订单数量
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status",Orders.COMPLETED);

        //查询当日总营业额,当日已完成订单的总金额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        //统计有效订单数量,当日已完成订单的数量
        Integer validOrderCount = orderMapper.countByMap(map);

        //平均客单价
        Double unitPrice=0.0;
        //订单完成率
        Double orderCompletionRate=0.0;
        if (validOrderCount!=0 && totalOrderCount!=0){
            //订单完成率：有效订单数 / 总订单数
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice=turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers=userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 根据状态 查询订单管理数据
     * @return
     */
    public OrderOverViewVO getOverviewOrders() {

        Map map=new HashMap();
        map.put("begin",LocalDateTime.now().with(LocalTime.MIN));

        //待接单
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status",null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();

    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getOverviewDishes() {

        Map map=new HashMap();

        // 已启售数量
        map.put("status",StatusConstant.ENABLE);
        Integer sold=dishMapper.countBymap(map);

        // 已停售数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued=dishMapper.countBymap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO getOverviewSetmeals() {
        Map map=new HashMap();

        // 已启售数量
        map.put("status",StatusConstant.ENABLE);
        Integer sold=setmealMapper.countBymap(map);

        // 已停售数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued=setmealMapper.countBymap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

    }
}
